package edu.jmu.utils;

import java.util.List;

import edu.jmu.bean.system.MenuNode;

/**
 * 节点类  与 Json的转换
 * @author Neal
 *
 */
public class MenuNodes2Json {
	public static String menuNode2Json(List<MenuNode> nodes,boolean needUrl){
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		sb.append("{ id:0, pId:0, name:'OA', open:true, childOuter:false,dropRoot:false,drag:false},");
		for(MenuNode parentNode : nodes){			
			sb.append("{ id:").append(parentNode.getId()).append(",")
				.append("pId:0, name:'").append(parentNode.getName()).append("',")
				.append("position:"+parentNode.getPosition()).append(",");
			if(needUrl){
				sb.append("open:true,childOuter:false,dropRoot:false},");
			}else{
				sb.append("open:false,childOuter:false,dropRoot:false},");
			}
				
			for(MenuNode childNode : parentNode.getChildNodes()){
				sb.append("{ id:").append(childNode.getId()).append(",")
					.append("pId:").append(childNode.getParentNode().getId()).append(",")
					.append("position:"+childNode.getPosition()).append(",");
					if(needUrl){
						if(childNode.getUrl() != null){
							sb.append("url:'").append(childNode.getUrl()).append("',")
							.append("target:'mainFrame'").append(",");
						}
					}
					sb.append("name:'").append(childNode.getName()).append("'},");				
			}
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append("]");
		return sb.toString();
	}
	
	
}
